package com.dissertation.compoent;

import com.dissertation.model.Assets;
import com.dissertation.pojo.Message;
import com.dissertation.service.IApplyAllowService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 权限控制层
 */
@Component
@Aspect
public class AuthAOP {

    @Autowired
    IApplyAllowService applyAllowService;

    @Around( "execution(com.dissertation.pojo.Message com.dissertation.service.ISaveAssetsService.*(*))" )
    Object checkBeforeSave( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        Object arg = args[0];
        Assets assets = (Assets) arg;
        if ( applyAllowService.allow() ||
                applyAllowService.allow( assets )||applyAllowService.allUserAllow()) {
            return proceedingJoinPoint.proceed( args );
        } else return Message.createErr( "对不起，当前权限不足，只能由管理员和第一作者添加文档或者修改文档呢" );
    }


    @Around( "execution(com.dissertation.pojo.Message com.dissertation.service.impl.SelectService.findByNameAndId(..))" )
    Object checkBeforeSelect( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        Object arg = args[3]; // 对应id
        String id = (String) arg;
        if ( applyAllowService.allow() ||
                applyAllowService.allow( id ) ) {
            return proceedingJoinPoint.proceed( args );
        } else return Message.createErr( "对不起，当前权限不支持查看详情，请先申请" );
    }



    @Pointcut(value = "@annotation(com.dissertation.annotation.AdminAuth)")
    public void adminAuthAspect(){}

    @Around("adminAuthAspect()")
    public Object adminAuthProcess(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        Object[] args = proceedingJoinPoint.getArgs();
        if ( applyAllowService.allow()  ) {
            return proceedingJoinPoint.proceed( args );
        } else return Message.createErr( "对不起，当前权限不支持此功能" );
    }

    @Pointcut(value = "@annotation(com.dissertation.annotation.AdminOrTeacherAuth)")
    public void adminOrTeacherAuthAspect(){}

    @Around("adminOrTeacherAuthAspect()")
    public Object adminOrTeacherAuthProcess(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        Object[] args = proceedingJoinPoint.getArgs();
        if(applyAllowService.adminOrTeacherAllow()){
            return proceedingJoinPoint.proceed(args);
        }else return Message.createErr( "对不起，当前权限不支持此功能" );
    }

    @Pointcut(value = "@annotation(com.dissertation.annotation.AllUserAuth)")
    public void allUserAuthAspect(){}

    @Around("allUserAuthAspect()")
    public Object allUserAuthProcess(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        Object[] args = proceedingJoinPoint.getArgs();
        if(applyAllowService.allUserAllow()){
            return proceedingJoinPoint.proceed(args);
        }else return Message.createErr( "对不起，当前权限不支持此功能" );
    }

}
